Next Previous Table of Contents

The LInteger Class

#include "linteger.hxx"

Constructors

The Default Constructor

LInteger()

This constructor creates a LInteger with "NULL" magnitude. (This is not equivalent to zero magnitude in this library.) Be careful to assign a magnitude to the returned LInteger before using it other than as an lvalue.

A Constructor From Magnitude and Sign

LInteger::LInteger(const unsigned int* magnitude, const int numDigits, const int sign=0, const int copyMag=1)

This constructor creates a LInteger representing the number determined by the first three arguments. (See the Internal Representation of LIntegers section for details on the required format.) If copyMag is non-zero the new instance will make its own copy of the magnitude, so that the caller may retain ownership of the memory pointed to by magnitude. If copyMag is zero, the library will assume control of the memory, and will attempt to delete the memory pointed to by mangnitude upon destruction of the new instance.

Warning:

Be sure that the magnitude of any new LInteger you create with this constructor does not have lead zeroes. This is necessary for the internal consistency of the library. If you don't feel like checking if the magnitude you use has lead zeroes or not, call compress() immediately after creation of the new instance.

A Copy Constructor from other LIntegers

LInteger::LInteger(const LInteger& a)

This copy constructor creates a LInteger which represents the same integer that a does.

A Consturctor from unsigned ints

LInteger::LInteger(const unsigned int mag, const int sign=0)

Creates a LInteger of single digit magnitude whose sign is determined by the sign argument. (See the Internal Representation of LIntegers section for details on the required value.)

A Constructor from ints

LInteger::LInteger(const int mag)

Creates a LInteger representing the same integer that mag represents.

Public Methods

Compression Methods

It is imporant for the internal consistency of the library that operations always be done on LIntegers with no lead zeros in their magnitude. The two methods below are included to help programmers insure that a LInteger has no lead zeros. Normally, they will not need to be called, however, since all overloaded operators ensure that the instances they return or modify have no lead zeros.

compressable

inline int LInteger::compressable() const;

Returns a non-zero int iff the calling instance has lead zeros in its magnitude.

compress

void LInteger::compress()

Strips the lead zeros, if any, from the calling instance's magnitude and adjusts the size accordingly.

Protected Member Access Methods

sign

inline int LInteger::sign() const

Returns an integer representing the calling instance's sign. (See the Internal Representation of LIntegers section for details on the required value.)

setSign

inline void LInteger::setSign(const int newSign)

Sets the sign of the calling instance equal to the value given by newSign.

size

inline int LInteger::size() const

Returns an integer representing the calling instance's size.

setSize

inline void LInteger::setSize(const int newSize)

Sets the integer representing the number of digits in the calling instance's magnitude equal to the value given by newSize.

magnitude

inline unsigned int* LInteger::magnitude() const

Returns a pointer to the calling instance's magnitude.

Overloaded Operators

Assignment from a LInteger: a=b

LInteger& LInteger::operator=(const LInteger& a)

Makes the calling instance represent an integer equal to the integer represented by a and returns a reference to the modified calling instance.

Unary Minus: -a

friend inline LInteger operator-(const LInteger& a)

Returns a new LInteger representing the proudct of -1 and the integer represented by a.

Unary Plus: +a

friend inline LInteger operator+(const LInteger& a)

Returns a new LInteger representing the same integer that a represents. *snore*

Add: a+b

friend LInteger operator+(const LInteger& a , const LInteger& b)

Returns a new LInteger representing the sum of the integers represented by a and b.

Plus Equals: a+=b

inline LInteger& LInteger::operator+=(const LInteger& b)

Makes the calling instance represent the sum of the integer it represents before the call and the integer b represents. Returns a reference to the modified calling instance.

Postfix Increment: a++

inline LInteger& LInteger::operator++(int) // postfix

Makes the calling instance represent the integer one greater than the integer it represented before the call. Returns a reference to the modified calling instance.

Prefix Increment: ++a

inline LInteger& LInteger::operator++() // postfix

Makes the calling instance represent the integer one greater than the integer it represented before the call. Returns a reference to the modified calling instance. (Where have I heard this before? :) )

Minus: a-b

friend LInteger operator-(const LInteger& a , const LInteger& b)

Returns a new LInteger representing the integer represented by a minus the integer represneted by b.

Minus Equals: a-=b

inline LInteger& LInteger::operator-=(const LInteger& b)

Makes the calling instance represent the the integer it represents before the call minus the integer b represents. Returns a reference to the modified calling instance.

Postfix Decrement: a--

inline LInteger& LInteger::operator--(int)

Makes the calling instance represent the integer one less than the integer it represented before the call. Returns a reference to the modified calling instance.

Prefix Decrement: --a

inline LInteger& LInteger::operator--()

Makes the calling instance represent the integer one less than the integer it represented before the call. Returns a reference to the modified calling instance. (Sound famaliar? :) )

Multiply: a*b

friend LInteger operator*(const LInteger& a , const LInteger& b)

Returns a new LInteger representing the product of the integer represented by a and the integer represneted by b.

Times Equals: a*=b

inline LInteger& LInteger::operator*=(const LInteger& b)

Makes the calling instance represent the product of the integer it represents before and the integer that b represents. Returns a reference to the modified calling instance.

Div: a/b

friend LInteger operator/(const LInteger& a , const LInteger& b)

From number theory, it is know that given two integers a and b such that b is not zero, there exists q and r such that a=q*b+rwith 0<=r<|b|. a/b returns a LInteger representing the integer q given by this theorem.

Div Equals: a/=b

inline LInteger& LInteger::operator/=(const LInteger& b)

Makes the calling instance represent the integer the return value of a/b would represent (see directly above), and returns a reference to the modified calling instance.

Mod: a%b

friend LInteger operator%(const LInteger& a , const LInteger& b)

From number theory, it is known that given two integers a and b such that b is not zero, there exists q and r such that a=q*b+r and 0<=r<|b|. a%b returns a LInteger representing the integer r given by this theorem.

Mod Equals: a%=b

inline LInteger& LInteger::operator%=(const LInteger& b)

Makes the calling instance represent the integer the return value of a%b would represent (see directly above), and returns a reference to the modified calling instance.

Exclusive OR: a^b

friend LInteger operator^(const LInteger& a, const LInteger& b)

Returns a new LIntegers representing the integer whose ith most significant bit is equal to exclusive OR of the ith most significant bit of the integer represented by a and the ith most significant bit of the integer represented by b.

Note: under current implementation, a and b must represent non-negative integers with the same number of digits in their magnitude.

Exclusive OR Equals: a^=b

inline LInteger& LInteger::operator^=(const LInteger& b)

Makes the calling instance represent the integer the return value of a^b would represent (see directly above). Returns a reference to the modified calling instance.

Note: under current implementation, the calling instance and b must both represent non-negative integers with the same number of digits in their magnitude.

Inclusive OR: a|b

friend LInteger operator|(const LInteger& a, const LInteger& b)

Returns a new LIntegers representing the integer whose ith most significant bit is equal to inclusive OR of the ith most significant bit of the integer represented by a and the ith most significant bit of the integer represented by b.

Note: under current implementation, a and b must represent non-negative integers with the same number of digits in their magnitude.

Inclusive OR Equals: a|=b

inline LInteger& LInteger::operator|=(const LInteger& b)

Makes the calling instance represent the integer the return value of a|b would represent (see directly above). Returns a reference to the modified calling instance.

Note: under current implementation, the calling instance and b must both represent non-negative integers with the same number of digits in their magnitude.

AND: a&b

friend LInteger operator&(const LInteger& a, const LInteger& b)

Returns a new LIntegers representing the integer whose ith most significant bit is equal to the logical conjuction of the ith most significant bit of the integer represented by a and the ith most significant bit of the integer represented by b.

Note: under current implementation, a and b must represent non-negative integers with the same number of digits in their magnitude.

AND Equals: a&=b

inline LInteger& LInteger::operator&=(const LInteger& b)

Makes the calling instance represent the integer the return value of a&b would represent (see directly above). Returns a reference to the modified calling instance.

Note: under current implementation, the calling instance and b must both represent non-negative integers with the same number of digits in their magnitude.

Bitwise Negate: ~a

friend LInteger operator~(const LInteger& a)

Returns a new LInteger representing the integer whose ith most significant bit is the logical negation of the ith most singficant bit of the integer represent by a.

Shift Right: a>>distance

friend LInteger operator>>(const LInteger& a, const int distance)

Returns a new LInteger representing the integer whose ith most significant bit is equal to the i+distanceth most significant bit of the integer represented by a. distance can be much greater than the number of bits in an unsigned int.

Note: Under this implementation, distance must be non-negative.

Shift Right Equals: a>>=distance

inline LInteger& LInteger::operator>>=(const int distance)

Makes the calling instance represent the integer the return value of a>>distance would represent (see directly above). Returns a reference to the modified calling instance.

Note: the same notes as for >> apply. (See directly above.).

Shift Left: a<<distance

friend LInteger operator<<(const LInteger& a, const int distance)

Returns a new LInteger representing the integer whose ith least significant bit is equal to the i-distanceth least significant bit of the integer represented by a. If i-distance is not positive, the ith bit of the integer represented by returned LInteger will be zero. distance can be much greater than the number of bits in an unsigned int.

Note: Under this implementation, distance must be non-negative.

Shift Left Equals: a<<=distance

inline LInteger& LInteger::operator<<=(const int distance)

Makes the calling instance represent the integer the return value of a<<distance would represent (see directly above). Returns a reference to the modified calling instance.

Note: the same notes as for << apply. (See directly above.).

Less Than: a<b

friend inline int operator<(const LInteger& a, const LInteger& b)

Returns a non-zero int iff a is less than b.

Greater Than: a>b

friend inline int operator>(const LInteger& a, const LInteger& b)

Returns a non-zero int iff a is greater than b.

Equal To: a==b

friend inline int operator==(const LInteger& a, const LInteger& b)

Returns a non-zero int iff a is equal to b.

Not Equal To: a!=b

friend inline int operator!=(const LInteger& a, const LInteger& b)

Returns a non-zero int iff a is not equal to b.

Less Than Or Equal To: a<=b

friend inline int operator<=(const LInteger& a, const LInteger& b)

Returns a non-zero int iff a is less than or equal to b.

Greater Than Or Equal To: a>=b

friend inline int operator>=(const LInteger& a, const LInteger& b)

Returns a non-zero int iff a is greater than or equal to b.

Logical Negation: !a

friend inline int operator!(const LInteger& a)

Returns a non-zero int iff a is non-zero.

Logical Conjuction: a&&b

friend inline int operator&&(const LInteger& a,const int b)

friend inline int operator&&(const int a,const LInteger& b)

Returns a non-zero int iff both of the arguments are non-zero.

Logical Disjunction: a||b

friend inline int operator||(const LInteger& a,const int b)

friend inline int operator||(const int a,const LInteger& b)

Returns a non-zero int iff at least one of the arguments is non-zero.

int Conversion: int(a)

inline LInteger::operator int()

Returns an int with the same sign as the calling instance, and with magnitude equal to the (bits per unsigned int)-1 least significant bits of the calling instance's magnitude.

Stream Output: s<<a

friend ostream& operator<<(ostream& s, const LInteger& a)

Pushes a onto the stream as follows: First, a '-' is pushed onto the stream if a is negative, otherwise a '+' is pushed onto the stream. The magnitude of a in hexadecimal is then pushed onto the stream.


Next Previous Table of Contents